#ruby 2.2.1p85 (2015-02-26 revision 49769)
#energies files must contain time on left column, energy on right, separated by space
#change paths on line 73

energies = Dir.glob('*energies.txt')
energies = energies.pop
text = File.open(energies).read
text = text.to_s
array = text.split("\n")

#creates time_arr which only holds time (same indices as main array)
time_arr =[]

array.each do |i|
    white = i.index(" ")
    white = white.to_i
    time_num = white - 1
    time_num = time_num.to_i
    time = i[0..time_num]
    time_arr.push(time)
end

puts time_arr

time_arr.map! {|x| " " + x + " "}
$cleaning_time = time_arr

$grep_arr = []

def file_checker(file_path, time_arr)
    puts " "
    count = 1
	to_del = []
	$this = file_path
    time_arr.each do |i|
        size = time_arr.size
        puts "Grepping time #{count} of #{size} in #{file_path}"
        grepped = File.open(file_path).read.to_s
        grepped_array = grepped.split("\n")
        count = count + 1
		
        grepped_array.each do |e|
            if e.include? i
				puts "Item found #{i}"
				mdcrd_path = file_path.gsub 'out', 'mdcrd'
				clean_str = e.scan(/\d+/).first
				math_str = clean_str.to_i
				#Divide by 500
				math_str = math_str/500
				new_str = "trajin #{mdcrd_path} #{math_str} #{math_str}"
                $grep_arr.push(new_str)
				to_del.push(i)
            end
        end
    end
	to_del.each do |f|
		time_arr.delete(f)
	end
	to_del = []
    return $grep_arr
end

Dir.glob('*out') do |stuff|
  results = file_checker(stuff, time_arr)
end

def answer()
    $grep_arr.each do |b|
    end
end

puts answer
stub = $this.split("_")[0]

#writes to new file
new = File.new("ptraj.in", "w")
new.puts answer
new.puts 
new.puts "trajout #{stub}.pdb pdb"
new.close